var ue;
$(function () {


    getWordNameList();

    getAllUpperInfo();


    // {
    //     initialContent: "在此填写简介@-@",
    //         enableAutoSave: false,
    //     pasteplain: true,
    //     toolbars: [
    //     ['fullscreen', 'source', 'undo', 'redo', 'bold']
    // ],
    //     autoHeightEnabled: true,
    //     autoFloatEnabled: true
    // }


    //简介符文版框
    ue = UE.getEditor('container');


    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action == 'uploadimage'  || action == 'uploadimage') {
            return "../../common/upload/UeditorUPFIle.json";
        } else if (action == 'uploadvideo') {
            return "../../../common/upload/UeditorUPFIle.json";
        } else
            if(action  =='uploadfile'){
            return "../../../common/upload/UeditorUPFIle.json"
        }else
            if(action  =='uploadscrawl'){
            return "../../../common/upload/scrawlUpload"
        }
        // if (action == 'listimage') {
        // return "${basePath}/download";
        // } else
        {
            return this._bkGetActionUrl.call(this, action);
        }
    }


    $(document).on("change", "#wordId_select", function () {
        $('#upperInfoId_select').empty();

        getAllUpperInfo();


    });


    $('#add').click(function () {
        $("#from_word_info").trigger("reset");
        add();
    });

    $('#update').click(function () {

        getAllUpperInfo();
        // 获取选中行信息
        var row = $('#table').bootstrapTable('getSelections');
        if (row == null || "" == row) {
            swal({
                title: "初始化失败!",
                text: "请选择行!",
                type: "error",
                confirmButtonText: "确定"
            });
        } else {
            edit(row[0]);
        }
    });

    $('#del').click(function () {
        // 获取选中行信息
        var row = $('#table').bootstrapTable('getSelections');
        if (row == null) {
            swal({
                title: "删除失败!",
                text: "请选择行!",
                type: "error",
                confirmButtonText: "确定"
            });
        } else {
            swal({
                title: "确定到删除",
                text: "确定要删除该行，删除后将无法回复",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false,
                closeOnCancel: true
            }, function (isConfirm) {
                if (isConfirm) {
                    del(row[0].id);
                }
            });

        }
    });




    // 加载数据
    $('#table')
        .bootstrapTable(
            {
                url: '../../wordInfoAction/main.json',
                pagination: true,
                height: 470,
                method: 'POST', //请求方式（*）
                contentType: "application/x-www-form-urlencoded",
                dataType: "json",
                pageSize: 10,
                pageList: [10, 20, 30],
                sidePagination: 'server',// 设置为服务器端分页
                pageNumber: 1,
                striped: true, // 是否显示行间隔色
                search: true, // 显示搜索框
                toolbar: '#toolbar', // 工具按钮用哪个容器
                sortable: true, // 是否启用排序
                showToggle: true,
                showColumns: true,
                showRefresh: true, // 是否显示刷新按钮
                selectItemName: 'btSelectItem',
                showExport: true, advancedSearch: true,
                idForm: 'form_search',
                actionForm: 'main.json',
                Locales: {
                    formatAdvancedSearch: '查询',
                    formatAdvancedCloseButton: '关闭'
                },
                idTable: 'table_search',
                uniqueId: "id",
                exportDataType: 'basic',
                clickToSelect: true, // 选择行即选择checkbox
                singleSelect: true, // 仅允许单选
                strictSearch: true,
                columns: [
                    {
                        align: 'center',
                        width: 50,
                        checkbox: true
                    },
                    {
                        field: 'id',
                        title: '编号',
                        align: 'center',
                        width: 15,
                        sortable: true
                    },
                    {
                        field: 'infoTitle',
                        title: '信息内容标题',
                        align: 'center',
                        sortable: true
                    },
                    {
                        field: 'wordId',
                        title: '所属词条',
                        align: 'center',
                        sortable: true,
                        // formatter: function (value, row, index) {
                        //     var html ="<li>"+getWordName(value)+"</li>";
                        //     return html;
                        // }
                    },
                    // {
                    //     field: 'infoDetails',
                    //     title: '文字介绍',
                    //     align: 'center',
                    //     sortable: true
                    // },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center',
                        sortable: true
                    }
                ]

            });


});

$('#btn_add').click(function () {
    if (!$('#from_word_info').valid()) {
        lrError("请输入必填项！");
        return;
    }
    var index = layer.load(0, {
        shade: false
    });
    $.ajax({
        type: "POST",
        url: "../../wordInfoAction/add.json",
        dataType: "json",
        data: $("#from_word_info").serialize(),
        success: function (data) {
            layer.closeAll('page');
            layer.close(index);
            emptyForm($('#from_word_info'));
            swal({
                title: "添加成功!",
                text: "用户添加成功!",
                type: "success",
                confirmButtonText: "确定"
            });
        },
        error: function () {
            layer.close(index);
            lrError("添加失败！服务器错误！");
        }
    });
});

$('#btn_update').click(function () {
    if (!$('#from_word_info').valid()) {
        lrError("请输入必填项！");
        return;
    }
    var index = layer.load(0, {
        shade: false
    });
    $.ajax({
        type: "POST",
        url: "../../wordInfoAction/update.json",
        dataType: "json",
        data: $("#from_word_info").serialize(),
        success: function (data) {
            swal({
                title: "修改!",
                text: "修改成功!",
                type: "success",
                confirmButtonText: "确定"
            });
            layer.closeAll('page');
            layer.close(index);
        },
        error: function () {
            layer.close(index);
            lrError("添加失败！服务器错误！");
        }
    });
});

// 删除
function del(id) {

    var index = layer.load(0, {
        shade: false
    });
    $.ajax({
        type: "POST",
        url: "../../wordInfoAction/del.json",
        dataType: "json",
        data: {
            "id": id
        },
        success: function (data) {
            $('#table').bootstrapTable('refresh');
            swal({
                title: "删除",
                text: "删除成功!",
                type: "success",
                confirmButtonText: "确定"
            });
            layer.close(index);

        },
        error: function () {
            lrError("删除失败！服务器错误！");
            layer.close(index);
        }
    });
}

// 添加操作
function add() {
    $("#btn_update").hide();
    $("#btn_add").show();
    var index_main = layer.open({
        zIndex:183,
        type: 1,
        title: '添加',
        shadeClose: true,
        shade: false,
        maxmin: true, // 开启最大化最小化按钮
        area: ['100%', '100%'],
        content: $('#div_word_info'),
        end: function () { // 此处用于演示
            $('#table').bootstrapTable('refresh');
        }
    });

    $('#btn_close').click(function () {
        layer.close(index_main);
    });

}


// 修改操作

function edit(row) {
    bindForm('from_word_info', row);
    $("#btn_add").hide();
    $("#btn_update").show();

    ue = UE.getEditor('container', {
        initialContent: row.infoDetails,
        enableContextMenu: false,
        autoHeightEnabled: false,
        initialFrameHeight: 480
    });


    var index_main = layer.open({
        zIndex:183,
        type: 1,
        title: '修改信息',
        shadeClose: true,
        shade: false,
        maxmin: true, // 开启最大化最小化按钮
        area: ['100%', '100%'],
        content: $('#div_word_info'),
        end: function () { // 此处用于演示
            // swal("添加成功!", "活动信息保存成功", "success");
            $('#table').bootstrapTable('refresh');

        }
    });

    $('#btn_close').click(function () {
        layer.close(index_main);
    });

}

function getWordName(wordId) {
    var name;

    $.ajax({
        type: "POST",
        url: "../../wordMainAction/getWordNameByWordId.json",
        dataType: "json",
        data: {
            "wordId": wordId
        },
        success: function (data) {
            // name = data;
            // return name;
            return data;
        },
        error: function () {
            name = "error";
        }
    });



}

function getWordNameList() {
    $('#wordId_select').empty();

    $.ajax({
        type: "POST",
        url: "../../wordMainAction/getAllWordList.json",
        dataType: "json",
        success: function (data) {
            $('#wordId_select').append("<option value='0'>...</option>");
            $.each(data.data.list, function (i, n) {
                $('#wordId_select').append("<option value='" + n.id + "'>" + n.wordName + "</option>");
            });
        },
        error: function () {
            name = "error";
        }
    });

}

function getAllUpperInfo() {
    var wordId = $("#wordId_select").val();
    $('#upperInfoId_select').empty();

    $.ajax({
        type: "POST",
        url: "../../wordInfoAction/getInfoListByWordId.json",
        dataType: "json",
        data: {
            "wordId": wordId
        },
        success: function (data) {
            $('#upperInfoId_select').append("<option value='0'>作为父信息</option>");
            $.each(data.data.list, function (i, n) {
                $('#upperInfoId_select').append("<option value='" + n.id + "'>" + n.infoTitle + "</option>");
            });
        },
        error: function () {
            name = "error";
        }
    });
}

function refreshTable() {
    $('#table').bootstrapTable('refresh');
}
